package cn.tedu.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

public class HelloScheduler {
    public static void main(String[] args) throws SchedulerException {
        //1.从工厂中获取scheduler对象
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        //2.定义一个Job Detail封装Job实现类
        JobDetail jobDetail = JobBuilder.newJob(HelloJob.class)
                .withIdentity("job1","group1") //给jobDetail命名，唯一
                .storeDurably() //持久化存储
                .build();
        //3.定义触发器，简单触发，每隔5秒触发一次
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger1","group1")//唯一的标识
                .startNow() //马上执行
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().repeatSecondlyForever(5)) //5秒执行一次
                .build();
        //4.使用scheduler将jobDetail和trigger绑定在一起
        scheduler.scheduleJob(jobDetail,trigger);
        //5.开启scheduler
        scheduler.start();
        //6.关闭
//        scheduler.shutdown();
    }
}
